﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using AMPLibrary.LastFM;

namespace MediaLibrary
{
    /// <summary>
    /// Interaction logic for ArtistInfoPage.xaml
    /// </summary>
    public partial class ArtistInformationPage : Page
    {
        private static ArtistInformationPage artistInformationPage;

        private ArtistInformationPage()
        {
            InitializeComponent();
        }

        public static ArtistInformationPage GetArtistInformationPage()
        {
            if (artistInformationPage == null)
                artistInformationPage = new ArtistInformationPage();

            return artistInformationPage;
        }

        public void BindArtistInformation(LastFMArtistInformation lastFMArtistInformation)
        {
            if (!lastFMArtistInformation.HasInformation)
                return;

            this.artistImage.Source = new BitmapImage(new Uri(lastFMArtistInformation.ImageMegaURL));
            this.artistNameTextBlock.Text = lastFMArtistInformation.ArtistName.ToLower();
            this.descriptionTextBlock.Text = lastFMArtistInformation.BioLong;

            tagsLabel.Text = "tags: ";

            foreach (string tag in lastFMArtistInformation.Tags)
                tagsLabel.Text += tag.ToLower() + ", ";

            tagsLabel.Text = tagsLabel.Text.Substring(0, tagsLabel.Text.Count() - 2);

            int i = 0;

            foreach (string artist in lastFMArtistInformation.SimilarArtists)
            {
                if (i == 0)
                    this.similarBand1.Content = artist;

                if (i == 1)
                    this.similarBand2.Content = artist;

                if (i == 2)
                    this.similarBand3.Content = artist;

                if (i == 3)
                    this.similarBand4.Content = artist;

                if (i == 4)
                {
                    this.similarBand5.Content = artist;
                    break;
                }

                i++;
            }

            this.eventListView.ItemsSource = lastFMArtistInformation.UpcommingEvents;
            this.eventListView.Items.Refresh();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void refreshButton_Click(object sender, RoutedEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Wait;

            LastFMArtistInformation lastFMArtistInformation = new LastFMArtistInformation();
            lastFMArtistInformation.TryPopulateLastFMArtistInformation(this.artistNameTextBlock.Text);

            if (lastFMArtistInformation.HasInformation)
                BindArtistInformation(lastFMArtistInformation);

            Mouse.OverrideCursor = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void similarBand_Click(object sender, RoutedEventArgs e)
        {
            Mouse.OverrideCursor = Cursors.Wait;

            System.Windows.Controls.Primitives.ToggleButton similarArtistToggleButton = (System.Windows.Controls.Primitives.ToggleButton)sender;

            if (similarArtistToggleButton == null)
                return;

            LastFMArtistInformation lastFMArtistInformation = new LastFMArtistInformation();
            lastFMArtistInformation.TryPopulateLastFMArtistInformation(similarArtistToggleButton.Content.ToString());

            if(lastFMArtistInformation.HasInformation)
                BindArtistInformation(lastFMArtistInformation);

            Mouse.OverrideCursor = null;
        }
    }
}
